package com.community.app.controller;

import com.community.app.entity.WxOwner;
import com.community.app.service.WxOwnerService;
import com.community.body.domain.ZyOwnerRoomRecord;
import com.community.body.domain.ZyRoom;
import com.community.body.service.IZyOwnerRoomRecordService;
import com.community.body.service.IZyRoomService;
import com.community.body.service.IZyUnitService;
import com.community.body.dto.CheckDTO;
import com.community.body.service.IZyOwnerRoomService;
import com.community.common.annotation.Anonymous;
import com.community.common.core.domain.AjaxResult;
import com.community.common.utils.file.FileUtils;
import com.community.common.utils.oss.AliyunOssUploadUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.community.common.utils.PageUtils.startPage;


/**
 * 业主 (ZyOwner)表控制层
 *
 * @author makejava
 * @since 2024-04-12 16:20:21
 */
@RestController
@RequestMapping("/mini/community/wx/zyOwner")
public class WxOwnerController {
    /**
     * 服务对象
     */
    @Resource
    private WxOwnerService wxOwnerService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private IZyUnitService unitService;
    @Resource
    private IZyRoomService roomService;
    @Resource
    private IZyOwnerRoomRecordService zyOwnerRoomRecordService;
    @Resource
    private IZyOwnerRoomService zyOwnerRoomService;


    /**
     * 登录
     *
     * @param wxOwner
     * @return
     */
    @RequestMapping("login")
    public AjaxResult login(@RequestBody WxOwner wxOwner) {
        int login = wxOwnerService.login(wxOwner);
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        String s = value.get(wxOwner.getOwnerPhoneNumber());
        if (login == 1) {
            return AjaxResult.success("登录成功", s);
        } else {
            return AjaxResult.error("账号或密码错误");
        }
    }

    /**
     * 注册
     *
     * @param wxOwner
     * @return
     */
    @Anonymous
    @RequestMapping("register")
    public AjaxResult register(@RequestBody WxOwner wxOwner) {
        //手机号校验
        String ownerPhoneNumber = wxOwner.getOwnerPhoneNumber();
        //手机号正则表达式
        String regExp = "^1[3|4|5|7|8][0-9]{9}$";
        //编译正则表达式
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regExp);
        //校验手机号
        java.util.regex.Matcher matcher = pattern.matcher(ownerPhoneNumber);
        if (!matcher.matches()) {
            return AjaxResult.error("手机号格式不正确");
        }
        //判断重复手机号
        int count = wxOwnerService.selectByPhone(ownerPhoneNumber);
        if (count > 0) {
            return AjaxResult.error("手机号已存在");
        }
        //注册
        int register = wxOwnerService.register(wxOwner);
        if (register == 1) {
            return AjaxResult.success("注册成功");
        } else {
            return AjaxResult.error("注册失败");
        }
    }

    /**
     * 根据手机号查询业主信息
     *
     * @param ownerPhoneNumber
     * @return
     */
    @RequestMapping("getOwnerByPhoneNumber")
    @ResponseBody
    public AjaxResult getOwnerByPhoneNumber(String ownerPhoneNumber) {
        WxOwner owner = wxOwnerService.getOwnerByPhoneNumber(ownerPhoneNumber);
        return owner == null ? AjaxResult.error("未查询到业主信息") : AjaxResult.success(owner);
    }

    /**
     * 根据手机号查询业主信息
     *
     * @param ownerId
     * @return
     */
    @RequestMapping("getOwnerRoom")
    @ResponseBody
    public AjaxResult getOwnerRoom(String ownerId) {
        WxOwner owner = wxOwnerService.getOwnerByPhoneNumber(ownerId);
        return owner == null ? AjaxResult.error("未查询到业主状态") : AjaxResult.success(owner);
    }

    /**
     * 修改业主信息
     *
     * @param wxOwner
     * @return
     */
    @Anonymous
    @RequestMapping("updateOwner")
    public AjaxResult updateOwner(@RequestBody WxOwner wxOwner) {

        int updateOwner = wxOwnerService.updateOwner(wxOwner);
        if (updateOwner == 1) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 退出
     *
     * @param phoneNumber
     */
    @RequestMapping("logout")
    public void logout(String phoneNumber) {
        System.out.println(phoneNumber);
        redisTemplate.delete(phoneNumber);
    }

    /**
     * 业主实名认证
     *
     * @param wxOwner
     * @return
     */
    @RequestMapping("realAuthentication")
    public AjaxResult realAuthentication(@RequestBody WxOwner wxOwner) {
        //验证身份证
        String ownerIdCard = wxOwner.getOwnerIdCard();
        //身份证正则表达式
        String regExp = "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        //编译正则表达式
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regExp);
        //校验身份证
        java.util.regex.Matcher matcher = pattern.matcher(ownerIdCard);
        if (!matcher.matches()) {
            return AjaxResult.error("身份证格式不正确");
        }
        wxOwnerService.realAuthentication(wxOwner);
        return AjaxResult.success("认证成功");
    }

    /**
     * 修改密码
     *
     * @param wxOwner
     * @return
     */
    @RequestMapping("updatePwd")
    public AjaxResult updatePwd(@RequestBody WxOwner wxOwner) {
        wxOwnerService.updatePwd(wxOwner);
        return AjaxResult.success("修改成功");
    }

    /**
     * 头像上传
     */
    @CrossOrigin
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        if (!file.isEmpty()) {
            String phoneNumber = request.getParameter("phoneNumber");
            String url = AliyunOssUploadUtils.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", FileUtils.getName(url));
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            if (wxOwnerService.updateUserAvatar(phoneNumber, url)) {
                ajax.put("imgUrl", url);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 查询可绑定的房间列表
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectRoomList")
    public AjaxResult RoomList(String id) {
        if (id == null) {
            return AjaxResult.error("id不能为空");
        }
        List<ZyRoom> roomList = roomService.selectBindingRoomList(id);
        return AjaxResult.success(roomList);
    }

    /**
     * 级联
     *
     * @return
     */
    @RequestMapping("/selectThree")
    public AjaxResult selectThree() {
        List<Object> list = unitService.selectZyList();
        return AjaxResult.success(list);
    }

    /**
     * 房屋绑定
     */
    @PostMapping("/bindRoom")
    public AjaxResult bindRoom(@RequestBody ZyOwnerRoomRecord zyOwnerRoomRecord) {
        int i = zyOwnerRoomRecordService.insertZyOwnerRoomRecord1(zyOwnerRoomRecord);
        if (i > 0) {
            return AjaxResult.success("绑定成功");
        }
        return AjaxResult.error("绑定失败");
    }

    /**
     * 查询房屋绑定列表
     */
    @GetMapping("/list")
    public AjaxResult list(CheckDTO checkDTO) {
        startPage();
        List<CheckDTO> list = zyOwnerRoomService.selectZyOwnerRoomList(checkDTO);
        return AjaxResult.success(list);
    }

    /**
     * 查询业主是否有房屋
     */
    @GetMapping("/getOwnerRoom")
    public AjaxResult getOwnerRoom(CheckDTO checkDTO) {
        List<CheckDTO> list = zyOwnerRoomService.selectZyOwnerRoomList(checkDTO);
        for (CheckDTO c : list) {
            if (c.getRoomStatus().contains("Binding")) {
                return AjaxResult.success(c);
            }
        }
        return AjaxResult.success(list);
    }
}

